package cn.bdqn.layui.service.product;

import cn.bdqn.layui.entity.Product;
import cn.bdqn.layui.entity.ProductType;
import cn.bdqn.layui.mapper.ProductMapper;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Resource
    ProductMapper productMapper;

    @Resource
    ProductDao productDao;

    @Override
    @Cacheable(cacheNames = "product", key = "getTargetClass()")
    public List<Product> getAllProductByPage() {
        System.out.println("查询数据库");
        return productMapper.getAllProductByPage();
    }

    @Override
    @CachePut(cacheNames = "product", key = "getTargetClass()")
    public List<Product> updateStack(Integer stack, Integer id) {
        productMapper.updateStack(stack, id);
        List<Product> allProductByPage = productMapper.getAllProductByPage();
        for (Product product : allProductByPage){
            productDao.save(product);
        }
        return allProductByPage;
    }

    @Override
    public List<ProductType> findProductType() {
        return productMapper.findProductType();
    }

    @Override
    @CachePut(cacheNames = "product", key = "getTargetClass()")
    public List<Product> addProduct(Product product) {
        productMapper.addProduct(product);
        return productMapper.getAllProductByPage();
    }
}
